#include "loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent) : QWidget{parent}
{
    // 1. 设置基本窗口属性
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(50, 0, 50, 0);
    this->setLayout(layout);

    // 3. 创建标题
    _titleLabel = new QLabel();
    _titleLabel->setText("登录");
    _titleLabel->setAlignment(Qt::AlignCenter);
    _titleLabel->setFixedHeight(50);
    _titleLabel->setStyleSheet("QLabel { font-size: 40px; font-weight: 600; }");

    // 4. 创建用户名输入框
    QString editStyle = "QLineEdit { border: none; border-radius: 10px; font-size: 20px; background-color: rgb(240, "
                        "240, 240); padding-left:5px; }";
    _usernameEdit     = new QLineEdit();
    _usernameEdit->setFixedHeight(40);
    _usernameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _usernameEdit->setPlaceholderText("输入用户名");
    _usernameEdit->setStyleSheet(editStyle);

    // 5. 创建密码输入框
    _passwordEdit = new QLineEdit();
    _passwordEdit->setFixedHeight(40);
    _passwordEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _passwordEdit->setPlaceholderText("输入密码");
    _passwordEdit->setStyleSheet(editStyle);
    _passwordEdit->setEchoMode(QLineEdit::Password);

    // 6. 创建验证码输入框
    _verifyCodeEdit = new QLineEdit();
    _verifyCodeEdit->setFixedHeight(40);
    _verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _verifyCodeEdit->setPlaceholderText("输入验证码");
    _verifyCodeEdit->setStyleSheet(editStyle);

    // 7. 显示验证码图片的控件（暂时用QPushButton代替）
    QPushButton *verifyCodeWidget = new QPushButton();
    verifyCodeWidget->setText("验证码");
    verifyCodeWidget->setStyleSheet("QWidget { border: none; }");

    // 8. 创建登录按钮
    _submitBtn = new QPushButton();
    _submitBtn->setText("登录");
    _submitBtn->setFixedHeight(40);
    _submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; background-color: rgb(44, 182, 61); "
                            "color: rgb(255, 255, 255); }";
    btnGreenStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _submitBtn->setStyleSheet(btnGreenStyle);

    // 9. 创建切换到手机号登录
    _phoneModeBtn = new QPushButton();
    _phoneModeBtn->setFixedSize(100, 40);
    _phoneModeBtn->setText("手机号登录");
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; background-color: transparent; }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _phoneModeBtn->setStyleSheet(btnWhiteStyle);

    // 10. 创建切换模式（登录和注册）按钮
    _switchModeBtn = new QPushButton();
    _switchModeBtn->setFixedSize(100, 40);
    _switchModeBtn->setText("注册");
    _switchModeBtn->setStyleSheet(btnWhiteStyle);

    // 11. 添加到布局管理器
    layout->addWidget(_titleLabel, 0, 0, 1, 5);
    layout->addWidget(_usernameEdit, 1, 0, 1, 5);
    layout->addWidget(_passwordEdit, 2, 0, 1, 5);
    layout->addWidget(_verifyCodeEdit, 3, 0, 1, 4);
    layout->addWidget(verifyCodeWidget, 3, 4, 1, 1);
    layout->addWidget(_submitBtn, 4, 0, 1, 5);
    layout->addWidget(_phoneModeBtn, 5, 0, 1, 1);
    layout->addWidget(_switchModeBtn, 5, 4, 1, 1);

    // 12. 连接信号槽
    connect(_switchModeBtn, &QPushButton::clicked, this, &LoginWidget::switchMode);
    connect(_phoneModeBtn, &QPushButton::clicked, this,
            [=]()
            {
                PhoneLoginWidget *phoneLoginWidget = new PhoneLoginWidget(nullptr);
                phoneLoginWidget->show();
                this->close();
            });
}

void LoginWidget::switchMode()
{
    if (_isLoginMode)
    {
        // 切换到注册模式
        this->setWindowTitle("注册");
        _titleLabel->setText("注册");
        _submitBtn->setText("注册");
        _switchModeBtn->setText("登录");
    }
    else
    {
        // 切换到登录模式
        this->setWindowTitle("登录");
        _titleLabel->setText("登录");
        _submitBtn->setText("登录");
        _switchModeBtn->setText("注册");
    }
    _isLoginMode = !_isLoginMode;
}
